import wx
import os

from pyeasyaccept.commom.Configuration import Configuration
from pyeasyaccept.commom.FacadeFactory import FacadeFactory

class ChooseFacadePanel(wx.Panel):
    """
    It's a panel that allows to choose the Facade that will be use to run the tests.
    
    @author: Artur Farias
    """
    
    facade = ''
    
    def __init__(self, parent, id, gridPos, space):
        """
        Initializes the panel.
        @param parent: the parent of the instance.
        @param id: the ID of the instance.
        @param gridPos: an array with the position for the grid.
        @param space: True for input a empty space, False if not. 
        """  
        wx.Panel.__init__(self, parent, id, size=wx.Size(500, 300))
        
        self.emptySpace = wx.StaticText(self, -1, '                                                 ')
        
        self.labelFacade = wx.StaticText(self, -1, 'Facade', (0,0))
        self.buttonFacade = wx.Button(self, 1, 'Browse')
        self.txtFacade = wx.TextCtrl(self, 1, '')
        size = wx.Size(310, self.txtFacade.GetSize()[1] )
        self.txtFacade.SetMinSize(size)

        self.Bind(wx.EVT_BUTTON, self.browseFacade, source=self.buttonFacade)
        
        self.grid = wx.GridBagSizer(9, 9)
        
        if space == True:
            self.grid.Add(self.emptySpace, (1,1), wx.DefaultSpan, wx.ALIGN_LEFT)
       
        self.grid.Add(self.labelFacade, ( gridPos[0], gridPos[1]), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.grid.Add(self.txtFacade, (gridPos[0] + 1, gridPos[1]), wx.DefaultSpan, wx.ALIGN_LEFT)
        self.grid.Add(self.buttonFacade, (gridPos[0] + 1, gridPos[1] + 1), wx.DefaultSpan, wx.EXPAND)
        
        self.SetSizer(self.grid)
        self.listeners = []
        
    def addListener(self, listener):
        self.listeners.append(listener)
        
    def reset(self):
        self.setFacade("")
                
    def browseFacade(self, event):
        """
        It receives an event from browse button. It allows to choose a Facade.
        @param event: a browse button event.
        """
        eventID = event.GetId()
        
        path = Configuration.PATH_TO_BROWSE

        dlg = wx.FileDialog(self, "Choose the Facade", path, "", "*.py", wx.OPEN)
        
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            self.setFacade(path)
            for listener in self.listeners:
                listener.notifySetFacade(self.getFacade())
        dlg.Destroy()
        
    def GetId(self):
        """
        Returns the ID of the instance.
        """
        return self.id
    
    def getFacade(self):
        """
        Returns the address of the Facade.
        """
        return self.facade
    
    def setFacade(self, path):
        """
        Set the Facade.
        """
        self.txtFacade.SetValue(path)
        self.txtFacade.SetInsertionPointEnd()
        
        self.facade = path